<template>
  <div id="ordersPayState">
    <h3>退保订单</h3>
    <div class="search">
      <el-select v-model="searchData.type" size="mini">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-input
        v-model="searchData.value"
        placeholder="请输入内容"
        size="mini"
      ></el-input>
    </div>
    <el-table :data="orderForPayState" border stripe style="width: 100%">
      <el-table-column prop="_id" label="订单编号"> </el-table-column>
      <el-table-column prop="ordersTime" label="创建时间"> </el-table-column>
      <el-table-column prop="customersId.name" label="订单客户">
      </el-table-column>
      <el-table-column prop="payPhone" label="支付手机"> </el-table-column>
      <el-table-column
        prop="ordersPayState"
        :formatter="PayState"
        label="订单状态"
      >
      </el-table-column>
      <el-table-column
        prop="ordersState"
        :formatter="State"
        label="理赔状态"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <p v-if="scope.row.ordersPayState == 2">
            <el-button
              size="mini"
              type="danger"
              @click="handleRelieve(scope.$index, scope.row)"
              >确定退保</el-button
            >
          </p>
          <p v-else-if="scope.row.ordersPayState == 3">
            <el-button size="mini" type="success">已退保</el-button>
          </p>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagesData.currentPage"
        :page-sizes="[1, 3, 5, 7]"
        :page-size="pagesData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagesData.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions, mapState, mapMutations } = createNamespacedHelpers(
  "Orders"
);
export default {
  data() {
    return {
      options: [
        {
          value: "ordersTime",
          label: "订单时间",
        },
        {
          value: "ordersPayState",
          label: "支付状态",
        },

        {
          value: "ordersState",
          label: "理赔状态",
        },
      ],
      searchData: {
        type: "ordersTime",
        value: "",
      },
    };
  },
  created() {
    this.getToCancelOrders(this.searchData);
  },

  computed: {
    ...mapState(["orderForPayState", "pagesData"]),
  },

  methods: {
    ...mapActions(["getToCancelOrders", "disposeOrdersToCancel"]),
    ...mapMutations(["changePageSize", "changeCurrentPage"]),

    PayState(row, column) {
      switch (row.ordersPayState) {
        case 0: {
          return "已支付";
          break;
        }
        case 1: {
          return "未支付";
          break;
        }
        case 2: {
          return "退保中";
          break;
        }
        case 3: {
          return "已退保";
          break;
        }
      }
    },

    State(row, column) {
      switch (row.ordersState) {
        case 0: {
          return "未理赔";
          break;
        }
        case 1: {
          return "理赔中";
          break;
        }
        case 2: {
          return "已理赔";
          break;
        }
      }
    },
    handleSizeChange(val) {
      this.changePageSize(val);
      this.getToCancelOrders(this.searchData);
    },

    handleCurrentChange(val) {
      this.changeCurrentPage(val);
      this.getToCancelOrders(this.searchData);
    },

    handleRelieve(index, row) {
      this.$confirm(" 是否确认此操作", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "已退保",
            duration: "800",
          });
          this.disposeOrdersToCancel(row._id);
          this.getToCancelOrders(this.searchData);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作",
            duration: "800",
          });
        });
    },
  },

  watch: {
    searchData: {
      handler(value) {
        this.changeCurrentPage(1);
        this.getToCancelOrders(value);
      },
      deep: true,
    },
  },
};
</script>

<style lang="scss">
#ordersPayState {
  h3 {
    margin-bottom: 10px;
  }
  .el-button {
    margin: 5px;
  }
  .el-table th,
  .el-table td {
    text-align: center;
    padding: 5px 0;
  }
  .search {
    display: flex;
    align-items: center;
    margin-bottom: 5px;
    .el-select {
      width: 100px;
      margin-right: 5px;
      .el-input {
        width: 100%;
      }
    }
    .el-input {
      width: 30%;
    }
  }
}
</style>